package com.ke.jun.view.widght

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.view.View
import com.ke.jun.view.R

/**
 * 画个动画玩玩
 */
class ZoombieView @JvmOverloads constructor
    (context: Context, attributeSet: AttributeSet?=null,
     defStyleAttr:Int=0): View(context,attributeSet,defStyleAttr) {
    private var intervalH=0
    private var intervalW=0
    private var widths=0
    private var heigt=0
    private var bitmapPaint=Paint()
    private var bitmap:Bitmap?=null
    private var src: Rect ?=null
    private var dst:Rect?=null
    private var currentW=0
    private var currentH=0;
    private val handler=object :Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (currentW<10&&currentH==0){
                currentW++
                invalidate()
                sendEmptyMessageDelayed(0,300)
            }else if(currentW==10&&currentH==0){
                currentH++
                currentW=0;
                invalidate()
                sendEmptyMessageDelayed(0,300)
            }else if (currentW<10&&currentH==1){
                currentH++
                invalidate()
                sendEmptyMessageDelayed(0,300)
            }
        }
    }


    init {
        bitmapPaint= Paint()
        with(bitmapPaint){
            isAntiAlias=true
            isDither=true
            isFilterBitmap=true
            color=Color.RED
        }

        bitmap=BitmapFactory.decodeResource(resources, R.mipmap.add_pic)
        intervalW = bitmap?.width?.div(11)!!
        intervalH= bitmap?.height?.div(2)!!
    }
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        widths=w
        heigt=h
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        canvas.translate((width/2).toFloat(), (heigt/2).toFloat())
//        src= Rect(
//            intervalW?.times(currentW)!!,
//            intervalH?.times(currentH)!!, intervalW!! *(currentW+1), intervalH!! *(currentH+1))
//
//       dst=Rect(-intervalW!! /2, intervalH!! /2, intervalW!! /2, intervalH!! /2)
//
//        canvas.drawBitmap(bitmap!!,src, dst!!,bitmapPaint)
//        if (currentH==0&&currentW==0){
//            handler.sendEmptyMessageDelayed(0,300)
//        }
        val text="柯军你好啊"
        bitmapPaint.textSize=18f
        canvas.drawText(text, 100F, 100F,bitmapPaint)
        bitmapPaint.textSize=28f
        canvas.drawText(text, 100F, 200F,bitmapPaint)
        bitmapPaint.textSize=38f
        canvas.drawText(text, 100F, 300F,bitmapPaint)
        bitmapPaint.textSize=48f
        canvas.drawText(text, 100F, 400F,bitmapPaint)
    }


}